package com.xzl.xep.service;

import java.util.HashMap;
import java.util.Map;

import com.xzl.xep.common.utils.DateUtil;
import com.xzl.xep.common.utils.StringUtil;
import com.xzl.xep.pojo.Customer;
import com.xzl.xep.pojo.Operator;
import com.xzl.xep.pojo.Payment;
import com.xzl.xep.pojo.Sale;

public class Order {

	private Integer isCustomer;
	
	private Long customerId;
	
	private Integer isCredit;
	
	private Double credit;
	
	private Double totalMoney;
	
	private Integer way;
	
	private String[] code;
	
	private String[] counts;

	public Integer getIsCustomer() {
		return isCustomer;
	}

	public void setIsCustomer(Integer isCustomer) {
		this.isCustomer = isCustomer;
	}

	public Long getCustomerId() {
		return customerId;
	}

	public void setCustomerId(Long customerId) {
		this.customerId = customerId;
	}

	public Integer getIsCredit() {
		return isCredit;
	}

	public void setIsCredit(Integer isCredit) {
		this.isCredit = isCredit;
	}

	public Double getCredit() {
		return credit;
	}

	public void setCredit(Double credit) {
		this.credit = credit;
	}

	public Double getTotalMoney() {
		return totalMoney;
	}

	public void setTotalMoney(Double totalMoney) {
		this.totalMoney = totalMoney;
	}

	public Integer getWay() {
		return way;
	}

	public void setWay(Integer way) {
		this.way = way;
	}

	public String[] getCode() {
		return code;
	}

	public void setCode(String[] code) {
		this.code = code;
	}

	public String[] getCounts() {
		return counts;
	}

	public void setCounts(String[] counts) {
		this.counts = counts;
	}
	
	public Sale getSale(Operator op,Customer customer){
		Sale sale = new Sale();
		
		sale.setCompanyId(op.getCompanyId());
		sale.setCompanyName(op.getCompanyName());
		sale.setCreateTime(DateUtil.getCurrentDate());
		if(customer != null){
			sale.setCustomerId(customer.getId());
			sale.setCustomerName(customer.getName());
		}
		
		sale.setIsCustomer(this.isCustomer);
		sale.setModifyTime(DateUtil.getCurrentDate());
		sale.setOperatorId(op.getId());
		sale.setOperatorName(op.getRealName());
		sale.setPayMoney(totalMoney);
		sale.setPreferenMoney(0d);
		sale.setSaleMoney(totalMoney);
		
		sale.setPayment(this.getPayment( customer, op));
		
		
		
		return sale;
	}
	
	public Payment getPayment(Customer customer, Operator op) {
		
		Payment payment = new Payment();
		
		payment.setBusiness(Payment.SALE_BUSINESS);
		payment.setCompanyId(op.getCompanyId());
		payment.setCreateTime(DateUtil.getCurrentDate());
		payment.setModifyTime(DateUtil.getCurrentDate() );
		payment.setMoney(this.totalMoney);
		payment.setOperatorId(op.getId());
		payment.setOperatorName(op.getRealName());
		payment.setType(1);
		payment.setRemark("消费");
		if(customer != null)
			payment.setCustomerId(customer.getId());
		
		if(hasCredit())
			payment.setWay(Payment.BALANCE_WAY);
		else
			payment.setWay(this.getWayString());
			
			
		return payment;
	}

	public Map<String,Integer> getDetail(){
		Map<String,Integer> map = new HashMap<String,Integer>();
		
		for(int i=0; i<code.length; i++){
			String c = code[i];
			String count = counts[i];
			if(StringUtil.isNotEmpty(c) && StringUtil.isNotEmpty(count) && Integer.parseInt(count) >0 )
				map.put(c,  Integer.parseInt(count));
		}
		
		return map;
	}
	
	
	public boolean hasCustomer(){
		if(this.isCustomer != null && this.isCustomer == 1 && this.customerId !=null && this.customerId >0 )
			return true;
		this.isCustomer = 0;
		return false;
	}
	
	public boolean hasCredit(){
		if(this.isCredit != null && this.isCredit == 1 && this.credit !=null && this.credit >0)
			return true;
		this.isCredit = 0;
		return false;
	}
	
	
	
	
	
	public String getWayString(){
		switch(way){
			case 0 : return Payment.CASH_WAY ; 
			case 1 : return Payment.ALIPAY_WAY ;
			case 2 : return Payment.BANK_WAY ;
			case 3 : return Payment.BALANCE_WAY ;
		}
		return null;
	}
	
	
	
	
}
